//
//  NewsViewController.m
//  Ui_13_HomeWork_网易新闻
//
//  Created by Bodi on 16/6/22.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "NewsViewController.h"
#import "Model.h"
#import "CustomTableViewCell.h"
#import "GetHeightTools.h"
#import "SecondTableViewCell.h"

@interface NewsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain) NSMutableArray<Model *> *array;
@property (nonatomic, retain)NSMutableArray *modelArray;

@end

@implementation NewsViewController
static NSString *const newsCellId = @"newsCell";
static NSString *const imageCellId = @"imageCell";
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    [self configNavigationItem];
    [self handleData];
    [self createTableView];

}
- (void)configNavigationItem{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"网易";
    /** 设置导航栏右边的按钮 */
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"搜索"] style:UIBarButtonItemStylePlain  target:self action:@selector(rightAction:)];
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"直播.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    
}
- (void)rightAction:(UIBarButtonItem *)item{
    NSLog(@"导航栏右边Item");
    
}
- (void)leftAction:(UIBarButtonItem *)item{
    NSLog(@"导航栏左边Item");
    
}

- (void)handleData{
    self.array = [NSMutableArray array];
    self.modelArray = [NSMutableArray array];
    NSString *path = [[NSBundle mainBundle]pathForResource:@"duanzi" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    NSArray *resultArray = [dictionary objectForKey:@"段子"];
    for (NSDictionary *dic in resultArray) {
        Model *model = [[Model alloc]init];
        [model setValuesForKeysWithDictionary:dic];
        [self.array addObject:model];
        [model release];
        NSLog(@"%@", self.array);
        
    }



}

- (void)createTableView{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    tableView.delegate = self;
    tableView.dataSource = self;
    
    
    
    [self.view addSubview:tableView];
    [tableView release];
    [tableView registerClass:[CustomTableViewCell class] forCellReuseIdentifier:newsCellId];
    [tableView registerClass:[SecondTableViewCell class] forCellReuseIdentifier:imageCellId];
    
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
        if ([self.array objectAtIndex:indexPath.row].img) {
            SecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:imageCellId];
             Model *model = self.array[indexPath.row];
            cell.model = model;
            return cell;
            
            
        
        }
         CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:newsCellId];
    
    Model *model = self.array[indexPath.row];
    
    cell.model = model;
    
      return cell;
        
        
        

        
   
        

    

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    Model *model = self.array[indexPath.row];
    
    return [GetHeightTools heightWith:model.digest] + 20;

}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
